package com.southminority.ethnic.controller.admin.vipadmin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southminority.ethnic.common.result.PageResult;
import com.southminority.ethnic.common.result.R;
import com.southminority.ethnic.controller.admin.vipadmin.vo.IdReqVo;
import com.southminority.ethnic.controller.admin.vipadmin.vo.UserPageReqVo;
import com.southminority.ethnic.controller.admin.vipadmin.vo.UserUpdateReqVo;
import com.southminority.ethnic.controller.user.vo.UserVO;
import com.southminority.ethnic.pojo.User;
import com.southminority.ethnic.service.user.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Tag(name = "超级管理员模块", description = "超级管理员模块")
@RestController
@RequestMapping("/vipadmin/user")
@CrossOrigin
public class UserAdminController {
    //分页根据关键词查看所有用户的信息
    @Autowired
    private UserService userService;

    @PostMapping("/page")
    @Operation(summary = "分页查询用户列表")
    public R<Page<User>> getUserPage(
            @RequestBody UserPageReqVo userPageReqVo
    ) {
        try {
            Page<User> pageResult = userService.getUserPage(
                userPageReqVo.getPageNum(),
                    userPageReqVo.getPageSize(),
                    userPageReqVo.getKeyword(),
                    userPageReqVo.getUserType());
            return R.ok(pageResult);
        } catch (Exception e) {
            log.error("分页查询用户失败", e);
            return R.fail("查询失败: " + e.getMessage());
        }
    }
    //根据用户id去更改用户的权限
    @PostMapping("/updateUserType")
    @Operation(summary = "根据用户id去更改用户的权限")
    public R<Boolean> updateUserType(
            @RequestBody UserUpdateReqVo userUpdateReqVo
    ) {
        try {
            Boolean user = userService.updateUser(
                    userUpdateReqVo.getId(),
                    userUpdateReqVo.getNewUserType()
            );
            return R.ok(user);
        } catch (Exception e) {
            log.error("根据用户id去更改用户的权限失败", e);
            return R.fail("查询失败: " + e.getMessage());
        }
    }
    //根据id删除用户
    @DeleteMapping("/deleteUser")
    @Operation(summary = "根据用户id删除用户")
    public R<Boolean> deleteUser(
            @RequestParam("id") Long id
    ) {
        try {
            return R.ok(userService.removeById(id));
        } catch (Exception e) {
            log.error("根据用户id删除用户失败", e);
            return R.fail("查询失败: " + e.getMessage());
        }
    }

    //根据id初始化密码
    @PostMapping("/initPassword")
    @Operation(summary = "根据用户id初始化密码")
    public R<Boolean> initPassword(
            @RequestParam("id") Long id
    ) {
        try {
            return R.ok(userService.initPassword(id));
        } catch (Exception e) {
            log.error("根据用户id初始化密码失败", e);
            return R.fail("初始化密码失败: " + e.getMessage());
        }
    }

}
